//created by: Justina Lee

Ext.define('MajorProjectSenchaTouch.controller.EResources', {
    extend: 'Ext.app.Controller',
    config:{
        views:['EResourcesModule.EResources', 'EResourcesModule.EResourcesList', 'EResourcesModule.EResourcesMusicList', 'EResourcesModule.EBooksSearch', 'EResourcesModule.eResourcesMain', 'EResourcesModule.EResourcesMusic', 'MainNavView'],
        refs:{
			navView: 'mainNavView',
            filterSelectField: '#filterSelectField',
            dropDownFilterMenu: 'button[action=dropdownfiltermenu]', 
            eResourcesFilter: 'button[action=showFilter]',
            eResourcesList: 'eResourcesList',
			eResourcesMusicList: 'eResourcesMusicList',
			eResourcesSelect: '#eResourcesFilter',
			eResEbooks: 'button[action=goEbooksSearch]', 
			eResDatabases: 'button[action=goEdatabases]',
			eResMusic: 'button[action=goEmusic]',
			mainBackButton: '#mainBackButton',
        },
        control:{
            filterSelectField :{
                change: 'filterCategory'
            },
            dropDownFilterMenu:{
                tap: 'showFilterMenu'
            },
            eResourcesFilter:{
                tap: 'showFilterPicker'
            },
            eResourcesList:{
                resetFilter: 'setToDefault',
                loadEResources : 'loadStore'
            },
			eResourcesMusicList:{
                loadEResourcesMusic : 'loadMusicStore'
            },
			eResEbooks:{
				tap: 'goEbooksSearch'
			},
			eResDatabases:{
				tap: 'goEdatabases'
			},
			eResMusic:{
				tap: 'goEmusic'
			}
        }
    },
/*
|--------------------------------------------------------------------------
| filterCategory (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| When the picker's value change, get the new value to filter the 
| EResources store.
|
*/
    filterCategory:function(selectfield, value, oldValue){
        var store = Ext.getStore('EResources');
        if(value != 'All'){
            store.filter('categoryName', value);
        }else{
            store.clearFilter();
        }
    },
/*
|--------------------------------------------------------------------------
| filterCategory (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Loads the EResources and EResourcesCategories when the user tap on the
| EResources tab.
|
*/
    loadStore: function(){
        var erStore = Ext.getStore('EResources');
        var categoryStore = Ext.getStore('EResourcesCategories');
        
        erStore.load();
        categoryStore.load();
    },
	
/*
|--------------------------------------------------------------------------
| loadMusicStore (@author : Sean Yow)
|--------------------------------------------------------------------------
|
| Loads the EResourcesMusic and EResourcesMusicCategory when the user tap on the
| Music tab.
|
*/
    loadMusicStore: function(){
        var erStore = Ext.getStore('EResourcesMusic');
        var categoryStore = Ext.getStore('EResourcesMusicCategory');
        
        erStore.load();
        categoryStore.load();
    },
/*
|--------------------------------------------------------------------------
| setToDefault (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Reset the selectfield picker to "All"
|
*/
    setToDefault: function(){
        var selectField = this.getFilterSelectField();
        selectField.setValue('All');
    },
/*
|--------------------------------------------------------------------------
| showFilterPicker (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Show the selectfield picker when the user tap on the settings button.
|
*/
    // showFilterPicker: function(){
        // var selectField = this.getFilterSelectField();
        // selectField.showPicker();
    // },
	
	// goMoveFocus: function(){
        // Ext.getCmp('Music').focus();
    // },
	
/*
|--------------------------------------------------------------------------
| goEbooksSearch (@author : Sean Yow)
|--------------------------------------------------------------------------
|
| This function is triggered when the user taps on the eBooks
| button at the eResources page. 
|
| It will navigate the user to the eBooks Search Page 
| whereby the user will enter their search condition   
|
*/
    goEbooksSearch: function(){
		var backBtn = this.getMainBackButton();
        var ctrl = this;
        var previousPage = ctrl.showMask();
       
        var task = Ext.create('Ext.util.DelayedTask', function(){
            ctrl.pushView('eBooks Search', 'basicebooksfilter');
            ctrl.hideMask(previousPage); 
            
            ctrl.applyNavUrl('eBooks_search');
        });
        task.delay(1);
		backBtn.setHidden(true);
    },
	
/*
|--------------------------------------------------------------------------
| goEdatabases (@author : Sean Yow)
|--------------------------------------------------------------------------
|
| This function is triggered when the user taps on the eDatabases
| button at the eResources page. 
|
| It will navigate the user to the eDatabases Page 
| whereby the user can browse the available eDatabases  
|
*/
    goEdatabases: function(){
		var backBtn = this.getMainBackButton();
        var ctrl = this;
        var previousPage = ctrl.showMask();
       
        var task = Ext.create('Ext.util.DelayedTask', function(){
            ctrl.pushView('eDatabases', 'eResourcescard');
            ctrl.hideMask(previousPage); 
            
            ctrl.applyNavUrl('eDatabases');
        });
        task.delay(1);
		backBtn.setHidden(true);
    },

/*
|--------------------------------------------------------------------------
| goEmusic (@author : Sean Yow)
|--------------------------------------------------------------------------
|
| This function is triggered when the user taps on the Music
| button at the eResources page. 
|
| It will navigate the user to the Music Page 
| whereby the user can browse Naxos Music Library
|
*/
    goEmusic: function(){
		var backBtn = this.getMainBackButton();
        var ctrl = this;
        var previousPage = ctrl.showMask();
       
        var task = Ext.create('Ext.util.DelayedTask', function(){
            ctrl.pushView('Music', 'eResourcesMusic');
            ctrl.hideMask(previousPage); 
            
            ctrl.applyNavUrl('Music');
        });
        task.delay(1);
		backBtn.setHidden(true);
    },
	
	/*
|--------------------------------------------------------------------------
| applyNavUrl (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Updates the url to the correct route fragment so it always matches the app's
| current state.
|
*/  
    
    applyNavUrl: function(urlLink){
        this.getApplication().getHistory().add(new Ext.app.Action({
            url: urlLink
        }), true);  
    },
	
    /*
|--------------------------------------------------------------------------
| hideMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Hide the loading mask of the page.
|
*/  
    hideMask: function(previousPage){        
        previousPage.setMasked(false);
    },
	
    /*
|--------------------------------------------------------------------------
| pushView (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Set the title of the navigation bar and push the page into the navigation
| view.
|
*/  
    pushView: function(setTitle, setXtype){
        var view = this.getNavView();
        
        view.push({
            title: setTitle,
            xtype: setXtype
        })
    },
	
    /*
|--------------------------------------------------------------------------
| showMask (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| Get the current item in the navigation view and set the loading mask.
| Return the current item as previousPage.
|
*/  
    showMask: function(){
        var view = this.getNavView();
        var previousPage = view._activeItem;
        
        previousPage.setMasked({
            xtype: 'loadmask',
            message: 'Please wait...'
        });
        
        return previousPage;
    },
});

